package com.lazy.book.service.system.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysRoleMapper;
import com.lazy.book.mapper.system.SysRoleMenuMapper;
import com.lazy.book.pojo.system.pojo.dto.common.SelectorQueryDTO;
import com.lazy.book.pojo.system.pojo.dto.sysrole.SysRoleCreateDTO;
import com.lazy.book.pojo.system.pojo.dto.sysrole.SysRoleListDTO;
import com.lazy.book.pojo.system.pojo.dto.sysrole.SysRoleUpdateDTO;
import com.lazy.book.pojo.system.pojo.po.SysRole;
import com.lazy.book.pojo.system.pojo.po.SysRoleMenu;
import com.lazy.book.pojo.system.pojo.vo.common.RoleVO;
import com.lazy.book.service.system.SysRoleService;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.PageUtils;
import com.sz.core.util.Utils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.lazy.book.pojo.system.pojo.po.table.SysRoleTableDef.SYS_ROLE;

/**
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public void create(SysRoleCreateDTO dto) {
        SysRole sysRole = BeanCopyUtils.copy(dto, SysRole.class);
        long count;
        count = QueryChain.of(SysRole.class).eq(SysRole::getRoleName, dto.getRoleName()).count();
        CommonResponseEnum.EXISTS.message("roleName已存在").assertTrue(count > 0);
        if (Utils.isNotNull(dto.getPermissions())) {
            count = QueryChain.of(SysRole.class).eq(SysRole::getPermissions, dto.getPermissions()).count();
            CommonResponseEnum.EXISTS.message("permissions已存在").assertTrue(count > 0);
        }
        save(sysRole);
    }

    @Override
    public void update(SysRoleUpdateDTO dto) {
        SysRole sysRole = BeanCopyUtils.copy(dto, SysRole.class);
        updateById(sysRole);
    }

    @Override
    public PageResult<SysRole> page(SysRoleListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_ROLE);
        if (Utils.isNotBlank(dto.getRoleName())) {
            wrapper.and(SYS_ROLE.ROLE_NAME.like(dto.getRoleName()));
        }
        Page<SysRole> page = page(dto.buildPage(), wrapper);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<SysRole> list(SysRoleListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_ROLE);
        if (Utils.isNotBlank(dto.getRoleName())) {
            wrapper.and(SYS_ROLE.ROLE_NAME.like(dto.getRoleName()));
        }
        return list(wrapper);
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertIsFalse(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SysRole detail(Object id) {
        return getById(id);
    }

    @Override
    public List<RoleVO> listSelector(SelectorQueryDTO dto) {
        return null;
    }

    @Override
    public Set<String> getRoleKeysByUserId(Long userId) {
        return null;
    }

    @Override
    public Set<String> getRoleNamesByUserId(Long userId) {
        return null;
    }
}